package me.spring.cloud.common.components.util.io;

import cn.hutool.core.io.FileUtil;
import me.spring.cloud.common.components.util.constant.CharConstant;
import me.spring.cloud.common.components.util.lang.AssertUtil;
import me.spring.cloud.common.components.util.lang.RandomUtil;
import me.spring.cloud.common.components.util.lang.StringUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 文件工具类
 * @author: luffy
 * @create: 2018-08-22 10:17
 **/
public class FileHelper extends FileUtil {

  private static final Pattern pattern = Pattern.compile("\\S*[?]\\S*");


  /**
   * @param filenameExtension 文件后缀
   * @Return String
   * @Description: 判断OSS服务文件上传时文件的contentType
   */
  public static String getcontentType(String filenameExtension) {
    if (filenameExtension.equals(".BMP") || filenameExtension.equals(".bmp")) {
      return "image/bmp";
    }
    if (filenameExtension.equals(".GIF") || filenameExtension.equals(".gif")) {
      return "image/gif";
    }
    if (filenameExtension.equals(".JPEG") || filenameExtension.equals(".jpeg") || filenameExtension.equals(".JPG")
        || filenameExtension.equals(".jpg") || filenameExtension.equals(".PNG")
        || filenameExtension.equals(".png")) {
      return "image/jpeg";
    }
    if (filenameExtension.equals(".HTML") || filenameExtension.equals(".html")) {
      return "text/html";
    }
    if (filenameExtension.equals(".TXT") || filenameExtension.equals(".txt")) {
      return "text/plain";
    }
    if (filenameExtension.equals(".VSD") || filenameExtension.equals(".vsd")) {
      return "application/vnd.visio";
    }
    if (filenameExtension.equals(".PPTX") || filenameExtension.equals(".pptx") || filenameExtension.equals(".PPT")
        || filenameExtension.equals(".ppt")) {
      return "application/vnd.ms-powerpoint";
    }
    if (filenameExtension.equals(".DOCX") || filenameExtension.equals(".docx") || filenameExtension.equals(".DOC")
        || filenameExtension.equals(".doc")) {
      return "application/msword";
    }
    if (filenameExtension.equals(".XML") || filenameExtension.equals(".xml")) {
      return "text/xml";
    }
    if (filenameExtension.equals(".apk") || filenameExtension.equals(".APK")) {
      return "application/octet-stream";
    }
    return "text/html";
  }

  /**
   * 获取链接的后缀名
   *
   * @Return
   */
  public static String parseSuffix(String url) {
    Matcher matcher = pattern.matcher(url);

    String[] spUrl = StringUtil.split(url, String.valueOf(CharConstant.SLASH));
    int len = spUrl.length;
    String endUrl = spUrl[len - 1];

    if (matcher.find()) {
      String[] spEndUrl = StringUtil.split(endUrl, String.valueOf(CharConstant.QUESTION_MARK));
      return StringUtil.split(spEndUrl[0], String.valueOf(CharConstant.DOT))[1];
    }
    return StringUtil.split(endUrl,
        StringUtil.append(String.valueOf(CharConstant.BACK_SLASH), String.valueOf(CharConstant.DOT)))[1];
  }

  public static String getReddemFile(String suffix) {
    AssertUtil.assertNotNullOrEmpty(suffix, "suffix is can't be null or empty");
    return System.getProperty("java.io.tmpdir") + RandomUtil.randomString(10) + CharConstant.DOT + suffix;
  }

}
